//
//  BVTableVCell.m
//  BuyVegetables1
//
//  Created by zhuzi on 16/11/22.
//  Copyright © 2016年 YSC. All rights reserved.
//  搜索界面的cell

#import "BVTableVCell.h"

#import "BVConst.h"
#import "UIImageView+WebCache.h"
#import "BVShopsLeftCategoryDetail.h"
#import "BVShopCart.h"

// 配图的尺寸
#define IconWH 80
#define COLOR_RGB(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
@interface BVTableVCell()<UITextFieldDelegate>

/**    标题*/
@property (nonatomic, strong) UILabel *titleLabel;
/**    内容*/
@property (nonatomic, strong) UILabel *contentLabel;
/**    配图*/
@property (nonatomic, strong) UIImageView *iconImage;
/**    斤数*/
@property (nonatomic, strong) UILabel *pounds;
/**    每袋多少元*/
@property (nonatomic, strong) UILabel *each_bag_money;
/**    每斤多少钱(详细单价)*/
@property (nonatomic, strong) UILabel *pounds_money;
/**    已选多少袋*/
@property (nonatomic, strong) UILabel *selectdBags;
/**    总重量*/
@property (nonatomic, strong) UILabel *allPounds;
/**    分割线*/
@property (nonatomic, strong) UIView *separatorLine;
/**    限购的数量*/
@property (nonatomic, strong) UILabel *xgCount;

/*
 * 是否限购
 */
@property (nonatomic, assign)NSInteger isXG;
/**
 *  限购时间Label
 */
@property (nonatomic,strong) UILabel *purchaseLabel;

@end

@implementation BVTableVCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
+ (instancetype)cell {
    return [[[NSBundle mainBundle] loadNibNamed:@"BVSearchShopsCell" owner:nil options:nil] lastObject];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        // 配图
        self.iconImage = [[UIImageView alloc] init];
        [self.contentView addSubview:self.iconImage];
        self.iconImage.layer.masksToBounds = YES;
        self.iconImage.layer.cornerRadius = 3.0;
        self.iconImage.sd_layout
        .topSpaceToView(self.contentView,10)
        .rightSpaceToView(self.contentView,10)
        .widthIs(80)
        .heightEqualToWidth();
        
        // 标题
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
        self.titleLabel.font = [UIFont systemFontOfSize:15];
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.sd_layout
        .rightSpaceToView(self.iconImage,10)
        .topSpaceToView(self.contentView,10)
        .leftSpaceToView(self.contentView,10)
        .autoHeightRatio(0);
        
        // 内容
        self.contentLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.contentLabel];
        self.contentLabel.font = [UIFont systemFontOfSize:12];
        self.contentLabel.numberOfLines = 3;
        self.contentLabel.textColor = BVBaseTextBlackColor;
        self.contentLabel.sd_layout
        .topSpaceToView(self.titleLabel,10)
        .leftSpaceToView(self.contentView,10)
        .rightSpaceToView(self.iconImage,10)
        .autoHeightRatio(0);
        
        // 每袋多少钱
        self.each_bag_money = [[UILabel alloc] init];
        self.each_bag_money.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:self.each_bag_money];
        self.each_bag_money.sd_layout
        .topSpaceToView(self.iconImage,10)
        .leftSpaceToView(self.contentView,10)
        .heightIs(12);
        [self.each_bag_money setSingleLineAutoResizeWithMaxWidth:100];
        
        // 斤数/规格
        self.pounds = [[UILabel alloc] init];
        [self.contentView addSubview:self.pounds];
        self.pounds.textColor = BVBaseOrangeColor;
        self.pounds.font = [UIFont systemFontOfSize:13];
        self.pounds.sd_layout
        .topSpaceToView(self.iconImage,10)
        .leftSpaceToView(self.each_bag_money,5)
        .heightIs(12);
        [self.pounds setSingleLineAutoResizeWithMaxWidth:100];
        
        //详细单价
        self.pounds_money = [[UILabel alloc]init];
        [self.contentView addSubview:self.pounds_money];
        self.pounds_money.font = [UIFont systemFontOfSize:12];
        self.pounds_money.textColor = BVBaseTextBlackColor;
        self.pounds_money.sd_layout
        .topSpaceToView(self.each_bag_money,5)
        .leftSpaceToView(self.contentView,10)
        .heightIs(12);
        [self.pounds_money setSingleLineAutoResizeWithMaxWidth:100];
        
        // 限购剩余数量
        self.xgCount = [[UILabel alloc] init];
        [self.contentView addSubview:self.xgCount];
        self.xgCount.font = [UIFont systemFontOfSize:12];
        self.xgCount.textColor = BVBaseOrangeColor;
        self.xgCount.sd_layout
        .topSpaceToView(self.pounds_money,5)
        .leftSpaceToView(self.contentView,10)
        .heightIs(15)
        .widthIs(100);
        
        // 添加商品
        self.addButton = [[UIButton alloc] init];
        [self.contentView addSubview:self.addButton];
        [self.addButton setImage:[UIImage imageNamed:@"sy_add"] forState:UIControlStateNormal];
        self.addButton.sd_layout
        .topSpaceToView(self.iconImage,10)
        .rightSpaceToView(self.contentView,10)
        .widthIs(30)
        .heightIs(25);
        
        // 已选多少个
        self.selectedCounts = [[UILabel alloc] init];
        [self.contentView addSubview:self.selectedCounts];
        self.selectedCounts.font = [UIFont systemFontOfSize:15];
        self.selectedCounts.textAlignment  = NSTextAlignmentCenter;
        self.selectedCounts.textColor = BVBaseOrangeColor;
        self.selectedCounts.sd_layout
        .topSpaceToView(self.iconImage,10)
        .rightSpaceToView(self.addButton,0)
        .heightIs(25)
        .widthIs(30);
        
        // 减少商品
        self.subButton = [[UIButton alloc] init];
        [self.contentView addSubview:self.subButton];
        [self.subButton setImage:[UIImage imageNamed:@"sy_minus"] forState:UIControlStateNormal];
        self.subButton.sd_layout
        .topSpaceToView(self.iconImage,10)
        .rightSpaceToView(self.selectedCounts,0)
        .widthIs(30)
        .heightIs(25);
        
        /**
         *  收藏按钮
         */
        self.collecButton = [[UIButton alloc] init];
        [self.contentView addSubview:self.collecButton];
        self.collecButton.sd_layout
        .topSpaceToView(self.addButton,5)
        .rightSpaceToView(self.contentView,120)
        .widthIs(20)
        .heightIs(20);
        
        /**
         *  收藏状态
         */
        self.collecStatusLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.collecStatusLabel];
        self.collecStatusLabel.font = [UIFont systemFontOfSize:12];
        self.collecStatusLabel.textColor = BVBaseOrangeColor;
        self.collecStatusLabel.text = @"收藏";
        self.collecStatusLabel.sd_layout
        .topSpaceToView(self.addButton,5)
        .rightSpaceToView(self.contentView,60)
        .widthIs(50)
        .heightIs(20);
        
        // 已选袋数
        self.selectdBags = [[UILabel alloc] init];
        [self.contentView addSubview:self.selectdBags];
        self.selectdBags.font  = [UIFont systemFontOfSize:12];
        self.selectdBags.textAlignment  = NSTextAlignmentRight;
        self.selectdBags.sd_layout
        .topSpaceToView(self.addButton,5)
        .leftSpaceToView(self.collecStatusLabel,5)
        .rightSpaceToView(self.contentView,10)
        .heightIs(20);
        
        // 分割线
        self.separatorLine = [[UIView alloc] init];
        [self.contentView addSubview:self.separatorLine];
        self.separatorLine.backgroundColor = [UIColor blackColor];
        self.separatorLine.alpha = 0.15;
        self.separatorLine.sd_layout
        .bottomSpaceToView(self.contentView,0)
        .leftSpaceToView(self.contentView,0)
        .rightSpaceToView(self.contentView,0)
        .heightIs(0.8);
        
        self.purchaseLabel = [[UILabel alloc]init];
        [self.contentView addSubview:self.purchaseLabel];
        self.purchaseLabel.font = [UIFont systemFontOfSize:12];
        self.purchaseLabel.backgroundColor = COLOR_RGB(255, 207, 191);
        self.purchaseLabel.sd_layout
        .bottomSpaceToView(self.separatorLine,5)
        .leftSpaceToView(self.contentView,10)
        .rightSpaceToView(self.contentView,10)
        .autoHeightRatio(0);
        
    }
    return self;
}

- (void)setSearchShops:(BVShopsLeftCategoryDetail *)searchShops {
    _searchShops = searchShops;
    // 标题
    self.titleLabel.text = searchShops.title;
    // 配图
    [self.iconImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", Path, searchShops.pic_name]]];
    // 内容
    if (searchShops.content.length >= 30 ) {
        NSString * str1 = @"...";
        NSString * str2 = [searchShops.content substringToIndex:30];
        NSLog(@"这是截取的===%@",str2);
        self.contentLabel.text = [NSString stringWithFormat:@"%@%@",str2,str1];
    }else{
        self.contentLabel.text = searchShops.content;
    }
    BVLog(@"这是开始的时间%@",searchShops.purchase_start_time);
    BVLog(@"这是结束的时间%@",searchShops.purchase_end_time);
    
    //截取开始时间字符串
    NSString *startTime = searchShops.purchase_start_time;
    NSArray  *arr = [startTime componentsSeparatedByString:@" "];
    NSString *startTimeW = [arr lastObject];
    NSString *startTimeY = [startTimeW substringToIndex:5];
    
    //截取结束时间字符串
    NSString *endTime = searchShops.purchase_end_time;
    NSArray  *arr2 = [endTime componentsSeparatedByString:@" "];
    NSString *endTimeW = [arr2 lastObject];
    NSString *endTimeY = [endTimeW substringToIndex:5];
    
    //判断字符串是否为 限购 如果不是限购就返回空字符串
    if (searchShops.isxg == 2) {
        self.purchaseLabel.text = [NSString stringWithFormat:@"此商品限购开始时间%@,截止时间%@",startTimeY,endTimeY];
    }else{
        self.purchaseLabel.text = @"";
    };
    
    // 每袋多少钱
    self.each_bag_money.text = [NSString stringWithFormat:@"%@元/%@", searchShops.each_bag_money,searchShops.each_bag_unit];
    
    // 斤数/规格
    self.pounds.text  = [NSString stringWithFormat:@"%@", searchShops.poundsunit];
    
    // 详细单价
    self.pounds_money.text = [NSString stringWithFormat:@" 每%@%@元",searchShops.pound_unit,searchShops.pounds_money];
    
    // 根据返回的type进行类型判断，看是否是限购,如果是就显示剩余数量label
    if (searchShops.isxg == 2) {
        self.xgCount.hidden = NO;
        self.xgCount.text = [NSString stringWithFormat:@"剩余数量：%@",searchShops.currentnum];
    }else if (searchShops.isxg == 1){
        self.xgCount.hidden = YES;
    }
    
    // 添加商品
    if ([searchShops.currentnum isEqualToString:@"0"] && searchShops.isxg == 2){
        self.addButton.hidden = YES;
    }else{
        self.addButton.hidden = NO;
    }
    
    BVShopsLeftCategoryDetail *shopDetail = [BVShopCart lookUpShop:searchShops];
    
    if (shopDetail != nil) {
        // 已选多少个
        self.selectedCounts.hidden = NO;
        self.selectedCounts.text   = [NSString stringWithFormat:@"%ld", (long)[BVShopCart shopCartCounts:searchShops]];
        // 减少商品
        self.subButton.hidden = NO;
    } else {
        self.selectedCounts.hidden = YES;
        self.subButton.hidden = YES;
    }
    
    // 已选袋数
    self.selectdBags.text  = [NSString stringWithFormat:@"已选%ld%@", (long)[BVShopCart shopCartCounts:searchShops],searchShops.each_bag_unit];
    
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    
//    if (self.delegate) {
//        [self.delegate bvSearchShopsCell:self shopsCountChanged:textField.text];
//    }
}
@end
